function swiper() {
    var mySwiper = new Swiper('.swiper', {
        direction: 'horizontal',
        loop: true, // 循环模式选项
        autoplay: true,
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
        // 如果需要前进后退按钮
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },

        // 如果需要滚动条
        scrollbar: {
            el: '.swiper-scrollbar',
        },
    })
}
//ajax请求列表数据
function product_list(pageNo = 1, pageSize = 8) {
    promise({
        url: `http://api.yuguoxy.com/api/shop/list`,
        method: "get",
        data: {
            pageSize,
            pageNo
        }
    }).then(data => {
        console.log(data);
        if (data.resultCode == 1)
            loding_list(data.resultInfo.list)
    }, err => {
        alert("加载数据出错")
    })
}
//渲染列表数据
function loding_list(list) {
    let ul = $(".product_list_main ul")
    $.each(list, function (index, value) {
        let li = $("<li>")
        li.html(`<a href="./product_details.html?id=${value.id}">
        <img src=${value.picture} alt="">
        <div>
            <p>${value.product}</p>
            <div class="clearfix"><span>淘宝</span> <span>包邮</span></div>
            <div class="clearfix">￥<span>${value.price}</span><span>${value.oldprice}人购买</span></div>
        </div>
    </a>`)
        ul.append(li)
    })
}
let pageNo = 1
//节流但是没用
const throttle = (fn, time) => {
    let flag = true
    return function () {
        if (flag == false)
            return flag = false
        setTimeout(() => {
            fn.apply(this)
            flag = true
        }, time)
    }
}
function trigger(fn, delay) {
    // 设置time为定时器，初始为 null
    var time = null;
     // 闭包原理，返回一个函数
    return function () {
      // 如果定时器存在清空定时器
      if (time) {
        clearTimeout(time);
      }
      // 设置定时器，规定时间后执行真实要执行的函数
      time = setTimeout(() => {
      // 将参数传递给真实执行的函数，因为箭头函数继承父级作用域，所以直接...arguments
        fn(...arguments);
      }, delay);
    };
  }
//动态加载滑到最底部加载
function scroll_loding() {
    $(window).scroll(function () {
        if ($(document.body).outerHeight() - $(window).scrollTop() - $(window).height() < 300) {
            throttle(product_list(++pageNo), 5000)
        }
    })
}
//请求获取轮播图
function bannerImg() {
    promise({
        url: `http://api.yuguoxy.com/api/shop/banner`,
        method: "get",
    }).then(data => {
        loding_banner(data.resultInfo.list)
    })
}
//轮播图加载
function loding_banner(list) {
    let $imgs = $(".swiper .swiper-wrapper img")
    console.log(list);
    $.each(list, function (index, value) {
        $imgs[index].src = value.url
    })
    swiper()
}
bannerImg()
scroll_loding()
product_list()
//去顶部
go_top()